package com.epoint.clubmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.epoint.clubmanagement.entity.Club;
import com.epoint.clubmanagement.mapper.ClubMapper;
import com.epoint.clubmanagement.service.IClubService;
import com.epoint.clubmanagement.vo.ClubVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Title: ClubServiceImpl
 * Description: 俱乐部service实现类
 *
 * @author sqn
 * @version 1.0.0
 * @date 2021/3/17 0017 下午 12:17
 */
@Service
public class ClubServiceImpl extends ServiceImpl<ClubMapper, Club> implements IClubService {

    @Autowired
    private ClubMapper clubMapper;

    /**
     * 分页查询俱乐部数据
     *
     * @param pageNum  第几页
     * @param pageSize 每页多少条数据
     * @param param    查询参数-俱乐部名称
     * @return
     */
    @Override
    public IPage<Club> selectClubPage(int pageNum, int pageSize, String param) {
        QueryWrapper<Club> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(param)) {
            queryWrapper.like("club_name", param);
        }
        Page<Club> page = new Page<>(pageNum, pageSize);
        return clubMapper.selectPage(page, queryWrapper);
    }

    /**
     * 分页查询俱乐部数据
     *
     * @param pageNum  第几页
     * @param pageSize 每页多少条数据
     * @param param    查询参数-俱乐部名称
     * @return
     */
    @Override
    public IPage<ClubVO> selectClubVOPage(int pageNum, int pageSize, String param) {
        QueryWrapper<ClubVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        if (StringUtils.isNotEmpty(param)) {
            queryWrapper.like("club_name", param);
        }
        Page<ClubVO> page = new Page<>(pageNum, pageSize);
        return clubMapper.queryClubVOByPage(page, queryWrapper);
    }

    /**
     * 新增一条俱乐部信息
     *
     * @param club
     */
    @Override
    @Transactional
    public int addClub(Club club) {
        return clubMapper.insert(club);
    }

    /**
     * 修改一条俱乐部信息
     *
     * @param club
     */
    @Override
    @Transactional
    public int editClub(Club club) {
        return clubMapper.updateById(club);
    }

    /**
     * 根据主键id查询一个俱乐部对象
     *
     * @param id
     * @return
     */
    @Override
    public Club queryClubById(Integer id) {
        return clubMapper.selectById(id);
    }

    /**
     * 根据主键id删除一个俱乐部对象
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int delClubById(Integer id) {
        return clubMapper.deleteById(id);
    }

    /**
     * 查询所有俱乐部
     *
     * @return
     */
    @Override
    public List<Club> queryClubList() {
        return clubMapper.selectList(null);
    }

    /**
     * 校验名称重复
     *
     * @param club
     * @return
     */
    @Override
    public int checkNameRepeatInsert(Club club) {
        QueryWrapper<Club> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("club_name", club.getClubName());

        return clubMapper.selectCount(queryWrapper);
    }

    /**
     * 校验名称重复
     *
     * @param club
     * @return
     */
    @Override
    public int checkNameRepeatUpdate(Club club) {
        QueryWrapper<Club> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("club_name", club.getClubName());
        if (StringUtils.isNotEmpty(String.valueOf(club.getId()))) {
            queryWrapper.ne("id", club.getId());
        }
        return clubMapper.selectCount(queryWrapper);
    }
}
